package com.brsya.movie.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.brsya.base.base.BaseMvpFragment;
import com.brsya.base.bean.InventoryBean;
import com.brsya.base.recycler.BaseAdapter;
import com.brsya.movie.R;
import com.brsya.movie.adapter.InventoryListAdapter;
import com.brsya.movie.contract.InventoryContract;
import com.brsya.movie.presenter.InventoryPresenter;
import com.brsya.movie.util.AppUtil;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/7/7  9:29
 * Description:
 * 片单列表
 */
public class InventoryFragment extends BaseMvpFragment<InventoryPresenter> implements InventoryContract.View {

    private SwipeRefreshLayout srlRefresh;
    private RecyclerView rlList;
    private TextView bottomTextView;


    private int titleId;

    private InventoryListAdapter adapter;

    public static InventoryFragment newInstance(int id) {

        Bundle args = new Bundle();
        args.putInt("titleId", id);
        InventoryFragment fragment = new InventoryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setArguments(@Nullable Bundle args) {
        super.setArguments(args);
        if (args != null) {
            titleId = args.getInt("titleId", -1);
        }
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_inventory);

        srlRefresh = findViewById(R.id.srl_refresh);
        rlList = findViewById(R.id.rl_list);
    }

    @Override
    protected void initData() {
        srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getInventoryList(titleId);
            }
        });

        rlList.setLayoutManager(new LinearLayoutManager(getContext()));
        rlList.setAdapter(adapter = new InventoryListAdapter(getContext()));

        View bottomView = LayoutInflater.from(getContext()).inflate(R.layout.item_no_more_data, null, false);
        bottomTextView = bottomView.findViewById(R.id.tv_no_more_data_text);
        adapter.setBottomView(bottomView);
        bottomTextView.setVisibility(View.GONE);

        adapter.setOnItemClickListener((position, bean) -> {
            String imgUrl;
            if (bean.getVideoList() != null && bean.getVideoList().size() > 0) {
                imgUrl = bean.getVideoList().get(0).getCoverUrl();
            }else{
                imgUrl = null;
            }
            AppUtil.openInventoryDetail(getContext(), bean.getHvId(), bean.getTitle(), imgUrl);
        });

        presenter.getInventoryList(titleId);
    }

    @Override
    protected void initPresenter() {
        presenter = new InventoryPresenter();
        presenter.attachView(this);
    }

    @Override
    public void showInventoryList(List<InventoryBean> inventoryList) {
        adapter.setDataList(inventoryList);
        adapter.notifyDataSetChanged();
        bottomTextView.setVisibility(View.VISIBLE);
    }

    @Override
    public void inventoryNoData() {
        bottomTextView.setVisibility(View.VISIBLE);
        bottomTextView.setText("数据为空");
    }

    @Override
    public void showLoading() {
        super.showLoading();
        srlRefresh.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        srlRefresh.setRefreshing(false);
    }
}
